<?php
/**
 * Created by PhpStorm.
 * User: koterwong
 * Date: 2017/8/30
 * Time: 18:59
 */

namespace app\api\model;

use think\Db;

/**
 * Class PortalCategoryModel  图片分类Model
 * @package app\api\model
 */
class PortalCategoryModel extends BaseModel
{


    /**
     * 获取一二级分类
     * @return array
     */
    public function getTree()
    {
        $map['status'] = 1;
        $map['delete_time'] = 0;
        $map['parent_id'] = 0;
        $result = Db::name('portal_category')
            ->where($map)
            ->field('id,name')
            ->order('list_order asc')
            ->select()
            ->toArray();
        foreach ($result as $key => $res) {
            $map2['parent_id'] = $res['id'];
            $map2['status'] = 1;
            $map2['delete_time'] = 0;
            $child = Db::name('portal_category')
                ->where($map2)
                ->field('id,name')
                ->order('list_order asc')
                ->select()
                ->toArray();
            foreach ($child as $k => $value) {
                $child[$key]['parent_id'] = $res['id'];
            }
            $result[$key]['child'] = $child;
        }

        return $result;
    }

    /**
     * 获取三级分类
     * @param $id
     * @return array
     */
    public function getChildrenById($id)
    {
        $result = $this
            ->field('id,name')
            ->where([
                'parent_id' => $id,
                'delete_time' => 0,
                'status' => 1,])
            ->select()
            ->toArray();
            
        return $result;
    }

    /**
     * 获取图片列表
     * @param $id
     */
    public function  getPhoto($id,$page,$page_size){

        if($page>1){
            $page = $page * $page_size;
        }
        $result =Db::name('portal_category_post ')
            ->where('category_id='.$id)->field('post_id')
             ->order('list_order asc')
            ->limit($page,$page_size)
            ->select()->toArray();
        $data = [];
        foreach ($result as $re){
            $data[] =$re['post_id'];
        }
        $map['id'] =array('in',$data);
        $map['post_status']= 1;
        $map['delete_time']= 0;
        $list=Db::name('portal_post')->where($map)->field('id,more')->order('is_top asc')->select()->toArray();
        foreach ($list as &$li){
            $more=json_decode($li['more']);
            $more=$this->object_to_array($more);
            $li['img_url']=cmf_get_image_preview_url($more['thumbnail']);
        }
        return $list;
    }

    /**
     * 查看图片，增加浏览量
     */
    public  function viewPhoto($id){
        Db::name('portal_post')->where('id='.$id)->setInc('post_hits',1);
        echo Db::name('portal_post')->getLastSql();
    }


    /**
     * 对象 转 数组
     *
     * @param object $obj 对象
     * @return array
     */
    function object_to_array($obj) {
        $obj = (array)$obj;
        foreach ($obj as $k => $v) {
            if (gettype($v) == 'resource') {
                return;
            }
            if (gettype($v) == 'object' || gettype($v) == 'array') {
                $obj[$k] = (array)object_to_array($v);
            }
        }

        return $obj;
    }

}